package com.dongdongshop.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSONObject;
import com.dongdongshop.Utils.CookieUtils;
import com.dongdongshop.domain.DataResult;
import com.dongdongshop.em.DataResultEnum;
import com.dongdongshop.pojo.Cart;
import com.dongdongshop.pojo.TbUser;
import com.dongdongshop.service.CartService;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("CartController")
public class CartController {


    @Reference
    private CartService cartService;


    @RequestMapping("toCart")
     public String toCart(){
         return "cart";
     }

    //没有登陆添加购物车
    @RequestMapping("saveCart")
    public String saveCart(HttpServletRequest request, HttpServletResponse response,Long itemId,Integer Num){
        TbUser principal = (TbUser)SecurityUtils.getSubject().getPrincipal();
        List<Cart> cart = getCart(request,response);
        List<Cart> list =  cartService.saveCart(cart,itemId,Num);
        if(principal == null){
            CookieUtils.setCookie(request,response,"cartList",JSONObject.toJSONString(list),true);
        }else{
            cartService.saveCartListToRedis(list,principal.getUsername());
        }
        return "success-cart";
    }

    @ResponseBody
    @RequestMapping("getCart")
    public List<Cart> getCart(HttpServletRequest request,HttpServletResponse response){
        TbUser principal = (TbUser)SecurityUtils.getSubject().getPrincipal();
        String textCookie = CookieUtils.getCookieValue(request, "cartList", true);
        if(StringUtils.isBlank(textCookie)){
            textCookie = "[]";
        }
        /*cookie*/
        List<Cart> cartscookie = JSONObject.parseArray(textCookie, Cart.class);
        if(principal == null){
            return cartscookie;
        }else{
            /*redis*/
            List<Cart> cartsredis = cartService.getCarListFormRedis(principal.getUsername());
            /*二次查看判断cookie中是否有数据*/
            if(cartscookie.size() > 0){
                /*合并后的*/
                List<Cart> carts = cartService.mergeCartList(cartsredis, cartscookie);
                /*Redis保存合并后的*/
                cartService.saveCartListToRedis(carts,principal.getUsername());
                /*合并后删除cookie中的*/
                CookieUtils.deleteCookie(request,response,"cartList");
                /*返回合并后的*/
                return carts;
            }else{
                /*cookie中没有直接返回redis中的*/
                return cartsredis;
            }
        }
    }

    @RequestMapping("addCartNum")
      @ResponseBody
      public DataResult addCartNum(HttpServletRequest request, HttpServletResponse response, Long id, Integer num){
         try {
             saveCart(request,response,id,num);
             return DataResult.buildDataResult(DataResultEnum.SUCCESS);
         }catch (Exception e){
             return DataResult.buildDataResult(DataResultEnum.ERROR);
         }
    }

}
